//
//  WYCMainHomeController.m
//  快付钱包
//
//  Created by 中企互联 on 2018/1/5.
//  Copyright © 2018年 中企互联. All rights reserved.
//

#import "WYCMainHomeController.h"
#import "LBBanner.h"
#import "XMGVerticalButton.h"
#import "PictureRotate.h"
#import "CCPScrollView.h"
#import "CCZTrotingLabel.h"
#import "WYCMymessegeController.h"
#import "WYCPictureModel.h"
#import "WYCBillModel.h"
#import "WYCHomeCell.h"
#import "WYCBillFrameModel.h"
#import "WYCSeeBillController.h"
#import "WYCHandBillController.h"
#import "WYCBillController.h"
#import "WYCRepaymentController.h"
#import "HWRollingView.h"
#import "WYCBankaController.h"
#import "WYCUserMessegeModel.h"
#import "WYCPufaViewController.h"
#import "WYCOneViewController.h"
#import "WYCTwoViewController.h"

@interface WYCMainHomeController ()<LBBannerDelegate,UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)NSMutableArray * imageArray;

@property (nonatomic, strong) CCZTrotingLabel *trotView;

/** 数组*/
@property(nonatomic,strong)NSMutableArray * showArray;

/** 数组*/
@property(nonatomic,strong)NSMutableArray * userArray;

/** 列表*/
@property(nonatomic,strong)UITableView * tableView;

/** view*/
@property(nonatomic,strong)UIView * billView;

/** 背景*/
@property(nonatomic,strong)UIButton * zView;

@property(nonatomic,strong)NSString  *code;

/** view*/
@property(nonatomic,strong)UIView * ejectView;

/** 显示View*/
@property(nonatomic,strong)UIView * showView;

/** view*/
@property(nonatomic,strong)UIView * lbview;

@property (nonatomic,strong) CCZTrotingLabel *label;

@property(nonatomic,strong)CALayer *bottomLineLayer;
@property(nonatomic,strong) XMGVerticalButton * packageButton ;
@property(nonatomic,strong) XMGVerticalButton * FangButton ;
@property(nonatomic,strong) XMGVerticalButton * BuyingButton ;
@property(nonatomic,strong) XMGVerticalButton * NetloanButton;
@property(nonatomic,strong) XMGVerticalButton * CardButton;
@property(nonatomic,strong)  XMGVerticalButton * billButton ;
@property(nonatomic,strong)UIImageView * imageView1;

@property(nonatomic,strong)UIView  *buttonView;
@end

@implementation WYCMainHomeController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = WYCGlobalBg;
    
    
    //图片数组
    self.imageArray = [[NSMutableArray alloc] init];
    
    self.userArray = [[NSMutableArray alloc] init];
    
    
    self.showArray = [[NSMutableArray alloc] init];
    
    //界面
    [self setupNav];
    
    
    //创建列表
    [self setupTableView];
    
    [self showloyout];
    
    [self setupIMG];
    
    
    [self setusermyMessage];
   [self loadversion];
}
-(void)loadversion
{
    
        NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];
    
        // 获取App的版本号
        NSString *appVersion = [infoDic objectForKey:@"CFBundleShortVersionString"];
        WYCLog(@"=======%@",appVersion);
    NSMutableDictionary * params = [NSMutableDictionary dictionary];
    
    params[@"version"] = appVersion;
    NSLog(@"%@",params);
    [HttpTool POST:function params:params success:^(NSURLSessionDataTask *task, id responseObject) {
        _code = responseObject[@"code"];
        NSLog(@"版本号%@",_code);
        if ([[responseObject objectForKey:@"code"] intValue ] == 0) {
           self. packageButton.hidden =YES;
           self.CardButton.hidden = YES;
           self. billButton.hidden =YES;
          _buttonView.frame = CGRectMake(0, 200, ScreenWidth, 500 * px);
            _imageView1.hidden =YES;
        }
        
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        
        [self showToast:Failed];
    }];
}

- (void)setusermyMessage{
    [self showLoadingWithMessage:@"加载中..."];
    
    NSString * str = [NSString stringWithFormat:@"%@token=%@",usermyMessage,[WYCAccountTool unarchivetoken]];
    [HttpTool POST:str params:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        WYCLog(@"-=----------%@",responseObject);
        if ([[responseObject objectForKey:@"code"]intValue] == 0) {
            [self.showArray removeAllObjects];
            [self hideLoading];
            NSArray * resultArray = responseObject[@"result"];
            for (NSDictionary * dict in resultArray) {
                WYCUserMessegeModel * usermodel = [WYCUserMessegeModel mj_objectWithKeyValues:dict];
                [self.showArray addObject:usermodel.content];
                
            }
            [self.label addTextArray:self.showArray];
            
            
        }else{
            [self hideLoading];
            [self showToast:msgStr];
        }
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
    
}
#pragma mark ------>>创建tableview
- (void)setupTableView{
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    
        self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, NavHeight, ScreenWidth, ScreenHeight - 20) style:UITableViewStylePlain];
        self.tableView.backgroundColor = WYCGlobalBg;
        self.tableView.dataSource = self;
        self.tableView.delegate = self;
        self.tableView.separatorStyle = UITableViewCellSelectionStyleNone;
    
        [self.view addSubview:self.tableView];
        
    }

- (void)showloyout{
    
    //    showView.backgroundColor = [UIColor redColor];
    
}

#pragma make ------->>>轮播图
- (void)setupIMG{
        
    [HttpTool POST:bannerlist params:nil success:^(NSURLSessionDataTask *task, id responseObject) {
            
        if ([[responseObject objectForKey:@"code"] intValue] == 0) {
                NSArray * resultarray = responseObject[@"result"];
                for (NSDictionary * dict in resultarray) {
                    
                    WYCPictureModel * model = [WYCPictureModel mj_objectWithKeyValues:dict];
                    [self.imageArray addObject:model.imgUrl];
                }
                //轮播图
            [self loadNetImageCustomPageControlAndTitle];
            
            }else{
                
                [SVProgressHUD showImage:[UIImage imageNamed:@""] status:msgStr];
            }
            
        } fail:^(NSURLSessionDataTask *task, NSError *error) {
            [SVProgressHUD showImage:[UIImage imageNamed:@""] status:Failed];
        }];
}
    
/**
 *  使用本地图片、系统PageControl、设置标题
 */
- (void)loadNetImageCustomPageControlAndTitle {
        
        UIView * cellview = [[UIView alloc] init];
        cellview.frame = CGRectMake(0, 0, ScreenWidth, 1200 * px);
        cellview.backgroundColor = [UIColor whiteColor];
        [self.view addSubview:cellview];
        
        
        HWRollingView * RollingView = [HWRollingView rollingViewWithFrame:CGRectMake(0, 0, ScreenWidth, 550 * px) RollingStyle:RollingStyleNetwork Data:self.imageArray];
        [cellview addSubview:RollingView];
        
        
    //通知的View
    UIView * showView = [[UIView alloc] init];
    showView.frame = CGRectMake(0, CGRectGetMaxY(RollingView.frame), ScreenWidth, WYCcellHeight);
    [cellview addSubview:showView];
    
    //通知imageView
    UIImageView *noticeimageView = [[UIImageView alloc] init];
    noticeimageView.frame = CGRectMake(80 * px, 60 * px, 40 * px, 40 * px);
    //    noticeimageView.backgroundColor = [UIColor blackColor];
    noticeimageView.image = [UIImage imageNamed:@"tongzhi"];
    [showView addSubview:noticeimageView];
    
    
    UIView * duanView = [[UIView alloc] init];
    duanView.backgroundColor = WYCRGBColor(205, 205, 205);
    duanView.frame = CGRectMake(CGRectGetMaxX(noticeimageView.frame) + 40 * px, 20 * px, 0.5, showView.height - 40 * px);
    [showView addSubview:duanView];
    
    self.label = [[CCZTrotingLabel alloc] init];
    self.label.frame = CGRectMake(CGRectGetMaxX(noticeimageView.frame) + 40 * px, noticeimageView.y - 20 * px, ScreenWidth - noticeimageView.width - 120 * px, 80 * px);
    self.label.direction = CCZTrotDirectionTop;
    self.trotView = self.label;
    self.label.repeatTextArr = YES;
    self.label.hideWhenStopTroting = YES;
    self.label.pause = 4;
    self.label.backgroundImage = [UIImage imageNamed:@"1"];
    // 添加滚动文本
    [self.label addTextArray:self.showArray];
    [showView addSubview:self.label];
    
        UIView * colorView = [[UIView alloc] init];
        colorView.backgroundColor = WYCGlobalBg;
        colorView.frame = CGRectMake(0, CGRectGetMaxY(showView.frame) , ScreenWidth, 30 * px);
        [cellview addSubview:colorView];
    
        
        _buttonView = [[UIView alloc] init];
        _buttonView.frame = CGRectMake(0, CGRectGetMaxY(colorView.frame), ScreenWidth, 1060 * px);
        _buttonView.backgroundColor = [UIColor whiteColor];
        [cellview addSubview:_buttonView];
    
    _BuyingButton = [[XMGVerticalButton alloc] init];
    _BuyingButton.frame = CGRectMake(120 * px, 80 * px, 160 * px, 230 * px);
    [_BuyingButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    
    [_BuyingButton addTarget:self action:@selector(BuyingButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [_BuyingButton setTitle:@"水果商城" forState:UIControlStateNormal];
    [_BuyingButton setImage:[UIImage imageNamed:@"che"] forState:UIControlStateNormal];
    [_buttonView addSubview:_BuyingButton];
    
    
    _FangButton = [[XMGVerticalButton alloc] init];
    _FangButton.frame = CGRectMake(_buttonView.width / 2 - 80 * px,80 * px, 160 * px, 230 * px);

    [_FangButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_FangButton setTitle:@"服装商城" forState:UIControlStateNormal];
    [_FangButton addTarget:self action:@selector(FangButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [_FangButton setImage:[UIImage imageNamed:@"fang"] forState:UIControlStateNormal];
    [_buttonView addSubview:_FangButton];
    //信用卡包
    _NetloanButton = [[XMGVerticalButton alloc] init];
    _NetloanButton.frame = CGRectMake(_buttonView.width - 300 * px, 80*px, 160 * px, 230 * px);
    [_NetloanButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_NetloanButton addTarget:self action:@selector(packageButtonClick) forControlEvents:UIControlEventTouchUpInside];
    
    [_NetloanButton setTitle:@"卡包" forState:UIControlStateNormal];
    [_NetloanButton setImage:[UIImage imageNamed:@"kabao"] forState:UIControlStateNormal];
    [_buttonView addSubview:_NetloanButton];
        
       _billButton= [[XMGVerticalButton alloc] init];
    _billButton.frame = CGRectMake(120 * px, _BuyingButton.frame.size.height+_BuyingButton.frame.origin.y+20, 160 * px, 230 * px);

        [_billButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_billButton addTarget:self action:@selector(billButtonClick)
             forControlEvents:UIControlEventTouchUpInside];
        _billButton.contentVerticalAlignment = UIControlContentHorizontalAlignmentCenter;
        [_billButton setTitle:@"智能还款" forState:UIControlStateNormal];
        [_billButton setImage:[UIImage imageNamed:@"huankuan"] forState:UIControlStateNormal];
        [_buttonView addSubview:_billButton];
        
        
        _CardButton = [[XMGVerticalButton alloc] init];
        _CardButton.frame = CGRectMake(_FangButton.x, _billButton.y, 160 * px, 230 * px);
        [_CardButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_CardButton setTitle:@"快捷办卡" forState:UIControlStateNormal];
        [_CardButton addTarget:self action:@selector(CardButtonClick) forControlEvents:UIControlEventTouchUpInside];
        [_CardButton setImage:[UIImage imageNamed:@"banka"] forState:UIControlStateNormal];
        [_buttonView addSubview:_CardButton];
    
        
       self.packageButton= [[XMGVerticalButton alloc] init];
       self. packageButton.frame = CGRectMake(_NetloanButton.x,_billButton.y, 160 * px, 230 * px);
        [self.packageButton addTarget:self action:@selector(NetloanButtonClick) forControlEvents:UIControlEventTouchUpInside];
        [self.packageButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [self.packageButton setTitle:@"网贷申请" forState:UIControlStateNormal];
        [self.packageButton setImage:[UIImage imageNamed:@"wangdai"] forState:UIControlStateNormal];
        [_buttonView addSubview:self.packageButton];
    
    
        
       _imageView1  = [[UIImageView alloc] init];
        _imageView1.frame = CGRectMake(40 * px, CGRectGetMaxY(self.packageButton.frame) + 50 * px, ScreenWidth - 80 * px, 660 * px);
        _imageView1.image = [UIImage imageNamed:@"main_bottom_bg"];
        [_buttonView addSubview:_imageView1];
        
        self.tableView.tableHeaderView = cellview;
        
}
#pragma make ------>>界面布局
- (void)setupNav{
    
    //添加轮播图
////    [self setupScrollView];
//    self.navigationItem.title = @"";
//    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
//    [self.navigationController.navigationBar setBarTintColor:WYCMain];
    
//    //左侧按钮
//    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithImage:@"ring" hight:@"ring" target:self action:@selector(photoClick)];
    
//    //右侧按钮
//    self.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithImage:@"add" hight:@"add" target:self action:@selector(addClick)];
    
    
    
}
#pragma make--------->>导入账单
- (void)showClick{
    WYCLogFunc;
    
}
#pragma make ------>>右侧按钮 点击事件
- (void)addClick{
    
    WYCLogFunc;
    
    
    UIView * ejectView = [[UIView alloc] init];
    ejectView.frame = CGRectMake(0, ScreenHeight - 500 * px, ScreenWidth, 500 * px);
    ejectView.backgroundColor = [UIColor whiteColor];
    [[UIApplication sharedApplication].keyWindow  addSubview:ejectView];
    self.ejectView = ejectView;
    
    
    UIButton * zView = [[UIButton alloc] init];
    zView.frame = CGRectMake(0, 0, ScreenWidth, ScreenHeight - ejectView.height);
    zView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
    [zView addTarget:self action:@selector(zviewClick) forControlEvents:UIControlEventTouchUpInside];
    [[UIApplication sharedApplication].keyWindow  addSubview:zView];
    self.zView = zView;
    
    UIButton * maillButton = [[UIButton alloc] init];
    maillButton.frame = CGRectMake(0, 25 * px, ejectView.width, 100 * px);
    [maillButton setTitle:@"邮箱导入" forState:UIControlStateNormal];
    maillButton.titleLabel.font = [UIFont systemFontOfSize:MiddleFont];
    [maillButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    maillButton.contentHorizontalAlignment =
    UIControlContentHorizontalAlignmentCenter;
    [maillButton addTarget:self action:@selector(maillButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [ejectView addSubview:maillButton];
    
    
    UIView * mallLine = [[UIView alloc] init];
    mallLine.backgroundColor = [UIColor grayColor];
    mallLine.frame = CGRectMake(0, CGRectGetMaxY(maillButton.frame) + 25 * px, ejectView.width, 0.5);
    [ejectView addSubview:mallLine];
    
    
    UIButton * holdButton = [[UIButton alloc] init];
    holdButton.frame = CGRectMake(0, CGRectGetMaxY(mallLine.frame) + 25 * px, ejectView.width, 100 * px);
    [holdButton setTitle:@"手输账单" forState:UIControlStateNormal];
    holdButton.titleLabel.font = [UIFont systemFontOfSize:MiddleFont];
    [holdButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    holdButton.contentHorizontalAlignment =
    UIControlContentHorizontalAlignmentCenter;
    [holdButton addTarget:self action:@selector(holdButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [ejectView addSubview:holdButton];
    
    
    UIView * line = [[UIView alloc] init];
    line.backgroundColor = WYCGlobalBg;
    line.frame = CGRectMake(0, CGRectGetMaxY(holdButton.frame) + 25 * px, ejectView.width, 20 * px);
    [ejectView addSubview:line];
    
    
    UIButton * NoButton = [[UIButton alloc] init];
    NoButton.frame = CGRectMake(0, CGRectGetMaxY(line.frame) + 25 * px, ejectView.width, 100 * px);
    [NoButton setTitle:@"取消" forState:UIControlStateNormal];
    NoButton.titleLabel.font = [UIFont systemFontOfSize:MiddleFont];
    [NoButton addTarget:self action:@selector(NoButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [NoButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    NoButton.contentHorizontalAlignment =
    UIControlContentHorizontalAlignmentCenter;
    [ejectView addSubview:NoButton];
    
}

//购车
- (void)BuyingButtonClick{
    NSLog(@"首付购房");
    WYCOneViewController * pufa = [[WYCOneViewController alloc] init];
    [self.navigationController pushViewController:pufa animated:NO];
}

//网贷申请
- (void)NetloanButtonClick{
    
    WYCPufaViewController * pufa = [[WYCPufaViewController alloc] init];
    [self.navigationController pushViewController:pufa animated:NO];
}
//购房
- (void)FangButtonClick{
    NSLog(@"首付购车");
    
    WYCTwoViewController * pufa = [[WYCTwoViewController alloc] init];
    [self.navigationController pushViewController:pufa animated:NO];
}

//智能还款
- (void)billButtonClick{
    
    WYCBillController * bii = [[WYCBillController alloc] init];
    [self.navigationController pushViewController:bii animated:NO];
}

//信用卡包
- (void)packageButtonClick{
    
    WYCRepaymentController * repay = [[WYCRepaymentController alloc] init];
    [self.navigationController pushViewController:repay animated:NO];
}

- (void)maillButtonClick{
    self.zView.hidden = YES;
    self.ejectView.hidden = YES;
    
    [SVProgressHUD showImage:[UIImage imageNamed:@""] status:@"开发中..."];
    
}

- (void)holdButtonClick{
    WYCLogFunc;
    self.zView.hidden = YES;
    self.ejectView.hidden = YES;
    WYCHandBillController * handbill = [[WYCHandBillController alloc] init];
    [self.navigationController pushViewController:handbill animated:NO];
}
- (void)CardButtonClick{
    WYCBankaController * bank = [[WYCBankaController alloc] init];
    [self.navigationController pushViewController:bank animated:NO];
    
}
- (void)NoButtonClick{
    WYCLogFunc;
    self.zView.hidden = YES;
    self.ejectView.hidden = YES;
    
}
- (void)zviewClick{
    WYCLogFunc;
    self.zView.hidden = YES;
    self.ejectView.hidden = YES;
    
}
#pragma make ------>>左侧按钮 点击事件
- (void)photoClick{
    WYCMymessegeController * mymessege = [[WYCMymessegeController alloc] init];
    [self.navigationController pushViewController:mymessege animated:NO];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    
    return 4;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    return 280 * px;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    return cell;
}

- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    
    self.navigationController.navigationBar.hidden = YES;
    
    
    UIView * NavView = [[UIView alloc] init];
    NavView.frame = CGRectMake(0, 0, ScreenWidth, NavHeight);
    NavView.backgroundColor = WYCMain;
    [self.view addSubview:NavView];
    
    
    
    self.bottomLineLayer = [[CALayer alloc] init];
    self.bottomLineLayer.frame = CGRectMake(0, NavView.bounds.size.height - 0.5, NavView.bounds.size.width, 0.5);
    self.bottomLineLayer.backgroundColor = WYCRGBColor(191, 191, 196).CGColor;
    [NavView.layer addSublayer:self.bottomLineLayer];
    
    
    //返回按钮
    UIButton *  backbutton = [[UIButton alloc] init];
    [backbutton setImage:[UIImage imageNamed:@"ring"] forState:UIControlStateNormal];
    [backbutton addTarget:self action:@selector(photoClick) forControlEvents:UIControlEventTouchUpInside];
    backbutton.frame = CGRectMake(30 * px, titleY, 66 * px , 66 * px);
    [NavView addSubview:backbutton];
    
    
    UILabel * NavRigsterLabel = [[UILabel alloc] init];
    NavRigsterLabel.frame = CGRectMake(0, titleY, NavView.width, 68 * px);
    NavRigsterLabel.textColor = [UIColor whiteColor];
    NavRigsterLabel.font = [UIFont boldSystemFontOfSize:BigFont];
    NavRigsterLabel.textAlignment = NSTextAlignmentCenter;
    NavRigsterLabel.text = @"首页";
    [NavView addSubview:NavRigsterLabel];
}

@end
